package top.api.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import top.api.domain.entity.LoginUserDetails;

/**
 * @Author 三更  B站： https://space.bilibili.com/663528522
 */
public class SecurityUtils
{

    /**
     * 获取用户
     **/
    public static LoginUserDetails getLoginUser()
    {
        return (LoginUserDetails) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 判断是否为管理员
     * @return
     */
    public static Boolean isAdmin(){
        // 用户类型：0代表普通用户，1代表管理员
        Long userId  = getLoginUser().getUser().getId();
        return userId.equals(1L);
    }

    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }
}